﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Data.Objects;
using System.Web.Profile;

namespace LULSMS
{
    public class UserManager
    {
        private static string createUser(string userName, string password, string email, string pswQ, string pswA, int citizenNumber, string name, string appName)
        {
            MembershipCreateStatus status;
            Membership.ApplicationName = appName;
            MembershipUser mu = Membership.CreateUser(userName, password, email, pswQ, pswA, true, out status);
            //Membership.UpdateUser(mu);
            
            ProfileBase pb = ProfileBase.Create(mu.UserName);


            pb.SetPropertyValue("FullName", name);
            pb.SetPropertyValue("CitizenNumber", citizenNumber);
            pb.Save();
            return status.ToString();
        }
        /// <summary>
        /// Criação de um Paciente
        /// </summary>
        /// <param name="userName">Utilizador Unico</param>
        /// <param name="password">Password utilizador</param>
        /// <param name="email">Email do utilizador</param>
        /// <param name="citizenNumber">Numero de cidadão</param>
        /// <param name="name">Nome</param>
        /// <param name="appName">Nome da aplicação</param>
        public static string createPatient(string userName, string password, string email, int citizenNumber, string name, string appName)
        {
            //Junta Role de paciente
            try
            {
                string status = createUser(userName, password, email, null, null, citizenNumber, name, appName);

                //TODO: 2 - Corrigir bug de associar mesmo que de erro ao criar user
                //TODO: DONE Fazer cena do CitizenNumber + FullName (Urgente)
                using (LUL_DBEntities entity = new LUL_DBEntities())
                {

                aspnet_Users u = entity.aspnet_Users.FirstOrDefault(i => i.UserName.Equals(userName));
                //    u.CitizenNumber = citizenNumber;

                Patients p = new Patients();
                p.aspnet_Users = u;

                entity.AddToPatients(p);
                entity.SaveChanges();
                }
                return status;
            }
            catch (Exception ex)
            {
                //TODO: Tratar excepções
                return ex.Message;
            }
        }

        /// <summary>
        /// Criação de um especialista
        /// </summary>
        /// <param name="userName">Utilizador Unico</param>
        /// <param name="password">Password utilizador</param>
        /// <param name="email">Email do utilizador</param>
        /// <param name="citizenNumber">Numero de cidadão</param>
        /// <param name="specialty">Nome</param>
        /// <param name="area">Especialidade</param>
        /// <param name="appName">Nome da aplicação</param>
        public static string createSpecialist(string userName, string password, string email, int citizenNumber, string name, string specialty, string appName)
        {
            try
            {
                string status = createUser(userName, password, email, null, null, citizenNumber, name, appName);
                //TODO: 2 - Como tratar das role de especialista


                using (LUL_DBEntities entity = new LUL_DBEntities())
                {
                    
                    //TODO: 3 - Modificar noutra versão. A Role está estática neste momento.
                    //verifica se role existe
                    
                    aspnet_Roles r = entity.aspnet_Roles.FirstOrDefault(i => i.LoweredRoleName.Equals(specialty.ToLower()));

                    aspnet_Users u = entity.aspnet_Users.FirstOrDefault(i => i.UserName.Equals(userName));
                    //u.CitizenNumber = citizenNumber;

                    Specialists s = new Specialists();
                    s.aspnet_Users = u;
                    s.aspnet_Roles = r;

                    entity.AddToSpecialists(s);
                    entity.SaveChanges();
                }
                return status;
            }
            catch (Exception ex)
            {
                //TODO: Tratar excepções
                return ex.Message;
            }
        }
    }
}
